
#include "stdAfx.h"

#include "UIObject.h"

namespace DuiLib
{
	CDuiObject::CDuiObject()
	{
	}

	CDuiObject::~CDuiObject()
	{
		RemoveAllAttributes();
		RemoveAllConnects();
	}

	void CDuiObject::RemoveAllConnects()
	{
		
	}

	void CDuiObject::WriteAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		CDuiObject::SetAttribute(pstrName, pstrValue);
	}

	void CDuiObject::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		if (pstrName == NULL || pstrName[0] == _T('\0')
			|| pstrValue == NULL || pstrValue[0] == _T('\0')) {
			return;
		}

		CDuiString* pAttr = new CDuiString(pstrValue);
		if (pAttr) {
			CDuiString* pOldAttr = (CDuiString*)m_Attributes.Set(pstrName, (LPVOID)pAttr);
			if (pOldAttr) {
				delete pOldAttr;
			}
		}
	}

	LPCTSTR CDuiObject::GetAttribute(LPCTSTR pstrName)
	{
		if (pstrName == NULL || pstrName[0] == _T('\0')) {
			return NULL;
		}

		CDuiString* pAttr = static_cast<CDuiString*>(m_Attributes.Find(pstrName));
		if (pAttr) {
			return pAttr->GetData();
		}

		return NULL;
	}

	const CStdStringPtrMap& CDuiObject::GetAllAttributes()
	{
		return m_Attributes;
	}

	bool CDuiObject::RemoveAttribute(LPCTSTR pstrName)
	{
		if (pstrName == NULL || pstrName[0] == _T('\0')) 
			return NULL;

		CDuiString* pAttr = static_cast<CDuiString*>(m_Attributes.Find(pstrName));
		if (!pAttr) 
			return false;

		delete pAttr;

		return m_Attributes.Remove(pstrName);
	}
	
	void CDuiObject::RemoveAllAttributes()
	{
		CDuiString* pAttr;
		for (int i = 0; i < m_Attributes.GetSize(); i++) {
			if (LPCTSTR key = m_Attributes.GetAt(i)) {
				pAttr = static_cast<CDuiString*>(m_Attributes.Find(key));
				delete pAttr;
			}
		}
		m_Attributes.Resize();
	}
}